package com.wisdytech.linkdcs.equipLifeCycle.maintain.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.DataTransformUtil;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.model.MaintenanceItem;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.service.IMaintenanceItemService;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.vo.MaintenanceItemRecordVO;
import com.wisdytech.linkdcs.equipLifeCycle.maintain.vo.MaintenanceItemVO;
import com.wisdytech.linkdcs.system.model.SysUser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;

/**
 * <p>
 *  保养项前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-06-09
 */
@Controller
@RequestMapping("/maintain/maintenanceItem")
public class MaintenanceItemController {
    /**
     * 设备类别service
     */
    @Autowired
    @Qualifier("iMaintenanceItemService")
    private IMaintenanceItemService iMaintenanceItemService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	/**
     * 
     * <p>Description: 设备保养</p>
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String maintainItem(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "lifeCycle/equipLifeCycle/maintain/item";
    }
    
    /**
     * 分页条件查询保养项信息
     * @param itemCode 备件编码
     * @param itemName 备件名称
     * @param filter 分页参数
     * @return 保养项列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listItemByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<MaintenanceItemVO> listItemByFilter(String itemCode, String itemName,Filter filter) throws IOException{
    	PageInfo<MaintenanceItem> items =this.iMaintenanceItemService.listItemByFilter(itemCode, itemName,filter);
        //转换为前台结果集
        List<MaintenanceItem> itemList=items.getList();
        List<MaintenanceItemVO> itemVOList=new ArrayList<MaintenanceItemVO>();
        for(MaintenanceItem maintenanceItem:itemList){
        	itemVOList.add(DataTransformUtil.changeItemVO(maintenanceItem));
        }
        return new StandardTableResult<MaintenanceItemVO>(items.getTotal(),itemVOList);
    }
    
    
    /**
     * <p>Description: 保存保养项对象</p> 
     * @param maintenanceItem 保养项对象
     * @param session 
     * @return 处理结果对象
     */
    @RequestMapping(value = "/saveItem/save.mvc")
    @ResponseBody
    public StandardResult saveItem(MaintenanceItem maintenanceItem, HttpSession session) {
       SysUser userSession = BaseUtil.getCurrentUser();
       String loginName = userSession.getUsername();
        if (StringUtils.isBlank(maintenanceItem.getPkId())) {
            iMaintenanceItemService.addItem(maintenanceItem, loginName);
        }else {
            iMaintenanceItemService.updateItem(maintenanceItem, loginName);
        }
        return StandardResult.getSuccessResult();
    }
    
    /**
     * <p>Description:查询保养项信息</p> 
     * @param pkId 
     * @return 保养项信息
     */
    @RequestMapping(value = "/getItemById/get.mvc")
    @ResponseBody
    public StandardResult getItemById(String pkId){
    	MaintenanceItem maintenanceItem = iMaintenanceItemService.getItemById(pkId);
        return StandardResult.getSuccessResult(maintenanceItem);
    }
    
    /**
     * 通过id批量删除备件信息
     * @param id 主键id
     * @return 删除结果集
     */
    @RequestMapping(value = "/delItemById/del.mvc")
    @ResponseBody
    public StandardResult delItemById(String pkId) {
    	iMaintenanceItemService.delItemById(pkId);
        return StandardResult.getSuccessResult();
    }
    
    /**
     * 分页条件查询保养计划中关联的保养项信息
     * @param pkId 备件名称
     * @param filter 分页参数
     * @return 保养项列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listPlanAndItemByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<MaintenanceItemVO> listPlanAndItemByFilter(String pkId,Filter filter) throws IOException{
    	PageInfo<MaintenanceItem> items =this.iMaintenanceItemService.listPlanAndItemByFilter(pkId,filter);
        //转换为前台结果集
        List<MaintenanceItem> itemList=items.getList();
        List<MaintenanceItemVO> itemVOList=new ArrayList<MaintenanceItemVO>();
        for(MaintenanceItem maintenanceItem:itemList){
        	itemVOList.add(DataTransformUtil.changeItemVO(maintenanceItem));
        }
        return new StandardTableResult<MaintenanceItemVO>(items.getTotal(),itemVOList);
    }
    
    /**
     * 分页条件查询保养记录明细表
     * @param pkId 备件名称
     * @param filter 分页参数
     * @return 保养项列表信息
     * @throws IOException 
     */
    @RequestMapping(value = "/listTaskConfirmItemByFilter/get.mvc")
    @ResponseBody	
    public StandardTableResult<MaintenanceItemRecordVO> listTaskConfirmItemByFilter(String pkId,Filter filter) throws IOException{
    	PageInfo<MaintenanceItem> items =this.iMaintenanceItemService.listTaskConfirmItemByFilter(pkId,filter);
        //转换为前台结果集
        List<MaintenanceItem> itemList=items.getList();
        List<MaintenanceItemRecordVO> itemVOList=new ArrayList<MaintenanceItemRecordVO>();
        for(MaintenanceItem maintenanceItem:itemList){
        	itemVOList.add(DataTransformUtil.changeItemRecordVO(maintenanceItem));
        }
        return new StandardTableResult<MaintenanceItemRecordVO>(items.getTotal(),itemVOList);
    }
    
	   /**
	    * <p>Description: 保存参数对象</p>
	    * @param file 上传的文件对象
	    * @return 处理结果对象
	    */
	   @RequestMapping(value = "/batchImportItem/save.mvc")
	   @ResponseBody
	   public StandardResult batchImportItem(@RequestParam(value = "file", required = false) MultipartFile file) {
	       if (file==null) {
	           return StandardResult.getFailResult("文件上传失败");
	       }
	       SysUser operator = BaseUtil.getCurrentUser();
	       this.iMaintenanceItemService.batchImportItem(file,operator);
	       return StandardResult.getSuccessResult("数据导入成功",null);
	   }
	   
	   
	   /**
	   *
	   * <p>Description: 下载文件</p>
	   */
	  @RequestMapping(value = "/downloadImportTemplate/get.mvc")
	  //@ResponseBody
	  public ResponseEntity<FileSystemResource> download() throws UnsupportedEncodingException {
	      //String filename = URLEncoder.encode("工装保养项导入模板.xlsx", StandardCharsets.UTF_8.displayName());
	      String filename = new String("保养项导入模板.xlsx".getBytes("gb2312"),"ISO-8859-1");
	      HttpHeaders headers = new HttpHeaders();
	      headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
	      headers.add("Content-Disposition", "attachment; filename=" + filename);
	      headers.add("Pragma", "no-cache");
	      headers.add("Expires", "0");
	      File file = null;
	      try {
	          file = ResourceUtils.getFile("classpath:static/excelTemplates/保养项导入模板.xlsx");
	      } catch (FileNotFoundException e) {
	          e.printStackTrace();
	          return null;
	      }
	      return ResponseEntity
	              .ok()
	              .headers(headers)
	              .contentLength(file.length())
	              .contentType(MediaType.parseMediaType("application/octet-stream"))
	              .body(new FileSystemResource(file));
	  }
}

